#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/8 17:12
# @Author  : jeff
# @FileName: exception_aspect
from src.vo.exception_vo import ValidationError
from src.vo.response import create_response
from src.enums.status_enum import Status


def business_exception(app):
    """
    Business exceptions are used to handle exceptions in business logic. Using flask's errorhandler decorator, you can
    capture specified exceptions.When a ValidationError exception occurs, the handle_base_error function is automaticall
    y called and the exception instance is passed to the function as a parameter.Therefore, we can directly use the
    exception instance error to call its internal to_response method and return the encapsulated API response object to
    the client.
    :param app:
    :return:
    """

    @app.errorhandler(ValidationError)
    def handle_base_error(error):
        return error.to_response()


def application_exception(app):
    """
    System exceptions are used to handle exceptions in applications. Using flask's errorhandler decorator, you can
    capture specified exceptions.
    :param app:
    :return:
    """

    @app.errorhandler(500)
    def handle_500_error(error):
        create_response(
            status=Status.BAD_MESSAGE,
            message='Server internal error',
            info=error
        )

    @app.errorhandler(404)
    def handle_404_error(error):
        create_response(
            status=Status.BAD_MESSAGE,
            message='Page does not exist',
            info=error
        )
